package hu.myai.triggers.bullish;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import hu.myai.api.tick.AbstractTickProvider;
import hu.myai.api.triggers.AbstractTrigger;
import hu.myai.indicators.RateOfChangeIndicator;
import hu.myai.indicators.WilliamsRIndicator;
import hu.myai.model.IndicatorVO;
import hu.myai.model.TriggerVO;

/**
 * 1. Moving average 14 and 42 cross. 14 goes from below to above (Up trend)<br>
 * 2. WilliamsR between 85 and 100<br>
 * 3. RSI > 50 (Up trend)<br>
 * 4. side trend and above the MVAs<br>
 * 
 * http://www.youtube.com/watch?v=T-AfwIvpN48&feature=related
 *
 * @author bozbalint
 *
 * */
public class WilliamsAndRocTrigger extends AbstractTrigger {

    private Double williamsHist;
    private Double rocHist;

    /**
     * The object used when logging debug,errors,warnings and info.
     * */
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    public WilliamsAndRocTrigger(String name, AbstractTickProvider tick) {
        super(name, tick);
    }

    private String williamsID;
    public void setWilliamsRIndicatorParameter(double parameter){
        williamsID = register(new WilliamsRIndicator(parameter));
    }
    
    private String rocID;
    public void setRateOfChangeIndicatorParameter(double parameter){
        rocID = register(new RateOfChangeIndicator(parameter));
    }

        
    @Override
    protected TriggerVO triggerCalculation() {

        IndicatorVO williamsIndicator = indicatorMap.get(williamsID);
        IndicatorVO rocIndicator = indicatorMap.get(rocID);

        // ignore calculation if no indicator value
        if (rocIndicator == null || williamsIndicator== null) {
            return null;
        }

        // init
        if (williamsHist == null || rocHist == null){
            williamsHist = williamsIndicator.getValue();
            rocHist = rocIndicator.getValue();
            return null;
        }

        TriggerVO trigger = null;
        // williams is below 50 and roc is nevative
        if(williamsIndicator.getValue() < -50 && rocIndicator.getValue() < 0)
            // williams and roc is rising
            if(williamsIndicator.getValue() > williamsHist && rocIndicator.getValue() > rocHist){
                trigger = new TriggerVO();
                trigger.setTick(lastTick);
                trigger.setRisk(20);
                trigger.setTradeType(TriggerVO.BUY_ONMARKET);

                log.debug(trigger.toString());
                
            }

        williamsHist = williamsIndicator.getValue();
        rocHist = rocIndicator.getValue();
        return trigger;
    }

}
